package com.googlecode.grima.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.grima.base.GenericDao;
import com.googlecode.grima.base.GenericEntityService;
import com.googlecode.grima.base.GenericEntityServiceImpl;
import com.googlecode.grima.base.Page;
import com.googlecode.grima.base.SearchResult;
import com.googlecode.grima.base.SearchTemplate;
import com.googlecode.grima.dao.ScheduleDao;
import com.googlecode.grima.domain.Schedule;

@Service("scheduleService")
public class ScheduleService extends GenericEntityServiceImpl<Schedule, Long> implements GenericEntityService<Schedule, Long> {

	protected ScheduleDao scheduleDao;

	@Autowired
	public void setScheduleDao(ScheduleDao scheduleDao) {
		this.scheduleDao = scheduleDao;
	}

	@Override
	public GenericDao<Schedule, Long> getDao() {
		return scheduleDao;
	}

	@Override
	public Schedule getNew() {
		return new Schedule();
	}

	@Override
	public Schedule getNewWithDefaults() {
		return getNew();
	}
	
	@Override
	@Transactional
	public void save(Schedule Schedule) {
		super.save(Schedule);
	}
	
	public SearchResult<Schedule, Long> findAll(Page page){
		SearchResult<Schedule, Long> ret = new SearchResult<Schedule, Long>();
		
		int pageNumber = page.getPageNumber();
		int pageSize = page.getPageSize();
		
    SearchTemplate searchTemplate = new SearchTemplate();
    int firstResult = ((pageNumber-1) * pageSize);
    searchTemplate.setFirstResult(firstResult);
    searchTemplate.setMaxResults(pageSize);
		List<Schedule> schedules = find(searchTemplate);
		int count = findCount(searchTemplate);

		ret.setTotal(count);
		ret.setRows(schedules);
		
		return ret;
	}
	
	public SearchResult<Schedule, Long> findByParameters(String code, String customer, String collection, String segment, Page page) {
		SearchResult<Schedule, Long> ret = new SearchResult<Schedule, Long>();
		
		int pageNumber = page.getPageNumber();
		int pageSize = page.getPageSize();
		
    SearchTemplate searchTemplate = new SearchTemplate();
    int firstResult = ((pageNumber-1) * pageSize);
    searchTemplate.setFirstResult(firstResult);
    searchTemplate.setMaxResults(pageSize);
    
    if (StringUtils.isNotEmpty(code)){
    	searchTemplate.addCriterion(Restrictions.like("code", code));
    }

    if (StringUtils.isNotEmpty(customer)){
    	searchTemplate.addCriterion(Restrictions.like("customer", customer));
    }

    if (StringUtils.isNotEmpty(collection)){
    	searchTemplate.addCriterion(Restrictions.like("collection", "%"+collection+"%"));
    }

    if (StringUtils.isNotEmpty(segment)){
    	searchTemplate.addCriterion(Restrictions.like("segment", "%"+segment+"%"));
    }

		List<Schedule> schedules = find(searchTemplate);
		int count = findCount(searchTemplate);

		ret.setTotal(count);
		ret.setRows(schedules);
		
		return ret;
  }
	
	public void deleteById(Long id){
		deleteByPrimaryKey(id);
	}

}
